package com.ayu.a.practice02;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * @Author 徐林玉
 * @Description IsAnagramPrac
 * @Date 2022/11/14 16:18
 * @Version 1.0
 */
public class IsAnagramPrac {

    public static void main(String[] args) {

        String s = "a";

        String t = "ab";

        boolean anagram = isAnagram(s, t);

        System.out.println("anagram = " + anagram);

    }


    public static boolean isAnagram(String s, String t) {

        if (s.length() != t.length()) {

            return false;
        } else {
            char[] c1 = s.toCharArray();

            char[] c2 = t.toCharArray();

            Map<Character, Integer> map1 = new HashMap<>();
            Map<Character, Integer> map2 = new HashMap<>();
            for (int i = 0; i < c1.length; i++) {

                char a1 = c1[i];

                char a2 = c2[i];
                Integer count1 = map1.get(a1);
                map1.put(a1, count1 == null ? 1 : count1 + 1);


                Integer count2 = map2.get(a2);
                map2.put(a2, count2 == null ? 1 : count2 + 1);
            }


            boolean flag = true;


            Set<Map.Entry<Character, Integer>> entries = map1.entrySet();


            for (Map.Entry<Character, Integer> entry :
                    entries) {

                Integer value = entry.getValue();

                Character key = entry.getKey();

                Integer value2 = map2.get(key);

                if(!value.equals(value2)){

                    flag = false;
                }

            }
            return flag;
        }


    }
}
